import api from '../../js/api';
import product from "../../js/product"
const dayjs = require('dayjs')
Page({

    data: {
        id: null,
        daiSaoMaList: [],
        yiSaoMaList: [],
        isShowCamera: false,
        audioErr: null,
        audioOk: null,
        wcode: null
    },

    onLoad(options) {
        console.log(options);
        let id = options.id
        this.setData({ id })
        this.loadDaiSaoMa()
        this.loadYiSaoMa()
        let err = wx.createInnerAudioContext();
        err.src = "/assets/audio/err.mp3"
        let ook = wx.createInnerAudioContext();
        ook.src = "/assets/audio/ok.mp3"

        let user = wx.getStorageSync('user')
        this.setData({
            audioErr: err,
            audioOk: ook,
            wcode: user.wcode
        })

    },

    loadDaiSaoMa() {
        let id = this.data.id
        api.getPeiZaiJianHuo_DaiSaoMa(id).then(r => {
            console.log(r);
            this.setData({ daiSaoMaList: r.data })
        })
    },
    loadYiSaoMa() {
        let id = this.data.id
        api.getPeiZaiJianHuo_YiSaoMa(id).then(r => {
            console.log(r);
            this.setData({ yiSaoMaList: r.data })
        })
    },
    scan: function(params) {
        let isShow = !this.data.isShowCamera;
        this.setData({
            containerPostion: "top",
            isShowCamera: isShow
        })
    },
    leave: function(params) {
        this.setData({
            isShowCamera: false
        })
    },
    scanRes: function(res) {
        let codeStr = res.detail.result
        if (this.data.lastCode == codeStr) {
            if ((new Date().getTime() - this.data.lastScanTime) / 1000 < 3) {
                return
            }
        }

        this.setData({ lastCode: codeStr, lastScanTime: new Date().getTime() })
        console.log(codeStr);

        let that = this

        let p = product.decode(codeStr)
        if (!p) {
            wx.showToast({
                title: '无法解析二维码',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }
        if (!p.skuno) {
            wx.showToast({
                title: '捆包号不能为空',
                icon: 'error',
                duration: 1000
            })
            this.data.audioErr.play()
            return
        }

        api.getPeiZaiJianHuo_Saoma(
            this.data.wcode,
            p.skuno,
            this.data.id,
            dayjs().format('YYYY-MM-DD HH:mm:ss')
        ).then(r => {
            this.data.audioOk.play()
            this.loadYiSaoMa()
            this.loadDaiSaoMa()
            wx.showToast({
                title: '扫码成功',
                icon: 'success',
                duration: 2000
            })

        })




    }

})